package com.shanxi.cz.module.biz.service;

import com.shanxi.cz.framework.common.util.calculate.AverageUtils;
import com.shanxi.cz.framework.common.util.date.LocalDateTimeUtils;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import com.shanxi.cz.module.biz.controller.admin.ammetermqtt.vo.AmmeterMqttPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecord.vo.AmmeterRecordPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecord.vo.AmmeterRecordSaveReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordday.vo.AmmeterRecordDayPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordday.vo.AmmeterRecordDaySaveReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth.vo.AmmeterRecordMonthPageReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordmonth.vo.AmmeterRecordMonthSaveReqVO;
import com.shanxi.cz.module.biz.controller.admin.ammeterrecordyear.vo.AmmeterRecordYearSaveReqVO;
import com.shanxi.cz.module.biz.dal.dataobject.ammetermqtt.AmmeterMqttDO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecord.AmmeterRecordDO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordday.AmmeterRecordDayDO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordmonth.AmmeterRecordMonthDO;
import com.shanxi.cz.module.biz.dal.dataobject.ammeterrecordyear.AmmeterRecordYearDO;
import com.shanxi.cz.module.biz.service.ammetermqtt.AmmeterMqttService;
import com.shanxi.cz.module.biz.service.ammeterrecord.AmmeterRecordService;
import com.shanxi.cz.module.biz.service.ammeterrecordday.AmmeterRecordDayService;
import com.shanxi.cz.module.biz.service.ammeterrecordmonth.AmmeterRecordMonthService;
import com.shanxi.cz.module.biz.service.ammeterrecordyear.AmmeterRecordYearService;
import com.shanxi.cz.module.infra.dal.dataobject.config.ConfigDO;
import com.shanxi.cz.module.infra.service.config.ConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Optional;

@Service
public class AmmeterServiceImpl implements AmmeterService {

    @Resource
    private AmmeterMqttService ammeterMqttService;

    @Resource
    private AmmeterRecordService ammeterRecordService;

    @Resource
    private AmmeterRecordDayService ammeterRecordDayService;

    @Resource
    private AmmeterRecordMonthService ammeterRecordMonthService;

    @Resource
    private AmmeterRecordYearService ammeterRecordYearService;

    @Resource
    private ConfigService configService;

    @Override
    public void nhAmmeterRecordJob() {
        ConfigDO configByKey = configService.getConfigByKey("ammeter.eng.multi");
        // 电流倍率
        String engMultiStr = configByKey.getValue();
        double engMulti = Double.parseDouble(engMultiStr);

        ConfigDO configByKeyVoltage = configService.getConfigByKey("ammeter.voltage.multi");
        // 电流倍率
        String voltageMultiStr = configByKeyVoltage.getValue();
        double voltageMulti = Double.parseDouble(voltageMultiStr);

        AmmeterMqttPageReqVO pageReqVO = new AmmeterMqttPageReqVO();
        // 当前日期
        String dateStr = LocalDateTimeUtils.getDateStr();
        pageReqVO.setRecordDate(dateStr);

        // 所有电表数据
        List<AmmeterMqttDO> ammeterMqttDOS = ammeterMqttService.listAmmeterMqtt(pageReqVO);

        // 主电表
        pageReqVO.setAmmeterId(1);
        List<AmmeterMqttDO> ammeterMqttMainS = ammeterMqttService.listAmmeterMqtt(pageReqVO);

        // 备用电表
        pageReqVO.setAmmeterId(2);
        List<AmmeterMqttDO> ammeterMqttStandbyS = ammeterMqttService.listAmmeterMqtt(pageReqVO);

        if (null != ammeterMqttMainS && !ammeterMqttMainS.isEmpty()) {
            // 主电表第一条数据和最新一条数据
            AmmeterMqttDO ammeterMqttMainNew = ammeterMqttMainS.get(0);
            AmmeterMqttDO ammeterMqttMainFirst = ammeterMqttMainS.get(ammeterMqttMainS.size() - 1);
            Integer idMainNew = ammeterMqttMainNew.getId();
            Integer idMainFirst = ammeterMqttMainFirst.getId();
            Double engMain;
            if(idMainNew.equals(idMainFirst)){
                engMain = ammeterMqttMainNew.getEng();
            }else{
                engMain = ammeterMqttMainNew.getEng() - ammeterMqttMainFirst.getEng();
            }

            // 备用电表第一条数据和最新一条数据
            Double engStandby = 0.00;
            if (null != ammeterMqttStandbyS && !ammeterMqttStandbyS.isEmpty()) {
                AmmeterMqttDO ammeterMqttStandbyNew = ammeterMqttStandbyS.get(0);
                AmmeterMqttDO ammeterMqttStandbyFirst = ammeterMqttStandbyS.get(ammeterMqttStandbyS.size() - 1);

                Integer idStandbyNew = ammeterMqttMainNew.getId();
                Integer idStandbyFirst = ammeterMqttMainFirst.getId();
                if(idStandbyNew.equals(idStandbyFirst)){
                    engStandby = ammeterMqttStandbyNew.getEng();
                }else{
                    engStandby = ammeterMqttStandbyNew.getEng() - ammeterMqttStandbyFirst.getEng();
                }
            }


            // 1.查询mqtt表中的数据生成电表记录
            String timeStr = LocalDateTimeUtils.getTimeStr();
            AmmeterRecordSaveReqVO ammeterRecordSaveReqVO = new AmmeterRecordSaveReqVO();
            ammeterRecordSaveReqVO.setAmmeterId(1);
            ammeterRecordSaveReqVO.setRecordTime(timeStr);
            ammeterRecordSaveReqVO.setRecordDate(dateStr);
            ammeterRecordSaveReqVO.setHz(ammeterMqttMainNew.getHz());
            ammeterRecordSaveReqVO.setVoltageA(BigDecimal.valueOf(voltageMulti * ammeterMqttMainNew.getUa()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setVoltageB(BigDecimal.valueOf(voltageMulti * ammeterMqttMainNew.getUb()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setVoltageC(BigDecimal.valueOf(voltageMulti * ammeterMqttMainNew.getUc()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setElectricityA(BigDecimal.valueOf(engMulti * ammeterMqttMainNew.getIa()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setElectricityB(BigDecimal.valueOf(engMulti * ammeterMqttMainNew.getIb()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setElectricityC(BigDecimal.valueOf(engMulti * ammeterMqttMainNew.getIc()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setPower(BigDecimal.valueOf(engMulti * voltageMulti * ammeterMqttMainNew.getPall()).setScale(2, BigDecimal.ROUND_UP).doubleValue());
//            ammeterRecordSaveReqVO.setEng(ammeterMqttDO.getEng());
            ammeterRecordSaveReqVO.setEng(BigDecimal.valueOf(engMulti * voltageMulti * (engMain + engStandby)).setScale(2, BigDecimal.ROUND_UP).doubleValue());
            ammeterRecordSaveReqVO.setPfs(ammeterMqttMainNew.getPfs());
            ammeterRecordSaveReqVO.setOperateType(1);
            ammeterRecordService.createAmmeterRecord(ammeterRecordSaveReqVO);

            // 2.计算电表数据日记录
            AmmeterRecordDaySaveReqVO ammeterRecordDaySaveReqVO;
            AmmeterRecordDayDO ammeterRecordDay = ammeterRecordDayService.getAmmeterRecordDay(1, dateStr);

            ammeterRecordDaySaveReqVO = new AmmeterRecordDaySaveReqVO();
            AmmeterRecordPageReqVO pageRecordReqVO = new AmmeterRecordPageReqVO();
            pageRecordReqVO.setAmmeterId(1);
            pageRecordReqVO.setRecordDate(dateStr);
            List<AmmeterRecordDO> ammeterRecordDOList = ammeterRecordService.selectByParams(pageRecordReqVO);
            if (null == ammeterRecordDay) {
                if (null != ammeterRecordDOList && !ammeterRecordDOList.isEmpty()) {
                    ammeterRecordDaySaveReqVO.setAmmeterId(1);
                    ammeterRecordDaySaveReqVO.setRecordDate(dateStr);
                    ammeterRecordDaySaveReqVO.setHz(ammeterRecordDOList.get(0).getHz());
                    ammeterRecordDaySaveReqVO.setVoltageA(ammeterRecordDOList.get(0).getVoltageA());
                    ammeterRecordDaySaveReqVO.setVoltageB(ammeterRecordDOList.get(0).getVoltageB());
                    ammeterRecordDaySaveReqVO.setVoltageC(ammeterRecordDOList.get(0).getVoltageC());
//                    ammeterRecordDaySaveReqVO.setVoltageAverage(ammeterRecordDOList.stream().map(x -> BigDecimal.valueOf(x.getVoltage())).reduce(BigDecimal.ZERO, BigDecimal::add)
//                            .divide(BigDecimal.valueOf(ammeterRecordDOList.size()), 2, RoundingMode.HALF_UP).doubleValue());
                    ammeterRecordDaySaveReqVO.setElectricityA(ammeterRecordDOList.get(0).getElectricityA());
                    ammeterRecordDaySaveReqVO.setElectricityB(ammeterRecordDOList.get(0).getElectricityB());
                    ammeterRecordDaySaveReqVO.setElectricityC(ammeterRecordDOList.get(0).getElectricityC());
//                    ammeterRecordDaySaveReqVO.setElectricityAverage(ammeterRecordDOList.stream().map(x -> BigDecimal.valueOf(x.getElectricity())).reduce(BigDecimal.ZERO, BigDecimal::add)
//                            .divide(BigDecimal.valueOf(ammeterRecordDOList.size()), 2, RoundingMode.HALF_UP).doubleValue());
                    ammeterRecordDaySaveReqVO.setPower(ammeterRecordDOList.get(0).getPower());
                    ammeterRecordDaySaveReqVO.setPowerFactor(ammeterRecordDOList.get(0).getPfs());
                    Optional<Double> max = ammeterRecordDOList.stream().map(AmmeterRecordDO::getPower).max(Double::compare);
                    ammeterRecordDaySaveReqVO.setPowerMax(max.get());
//                    ammeterRecordDaySaveReqVO.setEng(ammeterRecordDOList.get(0).getEng());
                    double eng = ammeterRecordDOList.get(0).getEng();
                    ammeterRecordDaySaveReqVO.setEng(eng);
                    double reductionDay = BigDecimal.valueOf(0.997 * eng).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    ammeterRecordDaySaveReqVO.setReduction(reductionDay);
                    double treePlantingDay = BigDecimal.valueOf(0.054 * eng).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    ammeterRecordDaySaveReqVO.setTreePlanting(treePlantingDay);

                    ammeterRecordDayService.createAmmeterRecordDay(ammeterRecordDaySaveReqVO);
                }
            } else {
                ammeterRecordDaySaveReqVO = BeanUtils.toBean(ammeterRecordDay, AmmeterRecordDaySaveReqVO.class);
                if (null != ammeterRecordDOList && !ammeterRecordDOList.isEmpty()) {
                    ammeterRecordDaySaveReqVO.setHz(ammeterRecordDOList.get(0).getHz());
                    ammeterRecordDaySaveReqVO.setVoltageA(ammeterRecordDOList.get(0).getVoltageA());
                    ammeterRecordDaySaveReqVO.setVoltageB(ammeterRecordDOList.get(0).getVoltageB());
                    ammeterRecordDaySaveReqVO.setVoltageC(ammeterRecordDOList.get(0).getVoltageC());
//                    ammeterRecordDaySaveReqVO.setVoltageAverage(ammeterRecordDOList.stream().map(x -> BigDecimal.valueOf(x.getVoltage())).reduce(BigDecimal.ZERO, BigDecimal::add)
//                            .divide(BigDecimal.valueOf(ammeterRecordDOList.size()), 2, RoundingMode.HALF_UP).doubleValue());
                    ammeterRecordDaySaveReqVO.setElectricityA(ammeterRecordDOList.get(0).getElectricityA());
                    ammeterRecordDaySaveReqVO.setElectricityB(ammeterRecordDOList.get(0).getElectricityB());
                    ammeterRecordDaySaveReqVO.setElectricityC(ammeterRecordDOList.get(0).getElectricityC());
//                    ammeterRecordDaySaveReqVO.setElectricityAverage(ammeterRecordDOList.stream().map(x -> BigDecimal.valueOf(x.getElectricity())).reduce(BigDecimal.ZERO, BigDecimal::add)
//                            .divide(BigDecimal.valueOf(ammeterRecordDOList.size()), 2, RoundingMode.HALF_UP).doubleValue());
                    ammeterRecordDaySaveReqVO.setPower(ammeterRecordDOList.get(0).getPower());
                    ammeterRecordDaySaveReqVO.setPowerFactor(ammeterRecordDOList.get(0).getPfs());
                    Optional<Double> max = ammeterRecordDOList.stream().map(AmmeterRecordDO::getPower).max(Double::compare);
                    ammeterRecordDaySaveReqVO.setPowerMax(max.get());
//                    ammeterRecordDaySaveReqVO.setEng(ammeterRecordDOList.get(0).getEng());
                    double eng = ammeterRecordDOList.get(0).getEng();
                    ammeterRecordDaySaveReqVO.setEng(eng);
                    double reductionDay = BigDecimal.valueOf(0.997 * eng).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    ammeterRecordDaySaveReqVO.setReduction(reductionDay);
                    double treePlantingDay = BigDecimal.valueOf(0.054 * eng).setScale(2, BigDecimal.ROUND_UP).doubleValue();
                    ammeterRecordDaySaveReqVO.setTreePlanting(treePlantingDay);
                    ammeterRecordDayService.updateAmmeterRecordDay(ammeterRecordDaySaveReqVO);
                }
            }


            // 3.计算电表数据月记录
            String monthStr = LocalDateTimeUtils.getMonthStr();
            AmmeterRecordDayPageReqVO pageRecordDayReqVO = new AmmeterRecordDayPageReqVO();
            pageRecordDayReqVO.setAmmeterId(1);
            pageRecordDayReqVO.setRecordDate(monthStr);

            List<AmmeterRecordDayDO> ammeterRecordDayByMonth = ammeterRecordDayService.getAmmeterRecordDayByMonth(pageRecordDayReqVO);
            AmmeterRecordMonthDO ammeterRecordMonth = ammeterRecordMonthService.getAmmeterRecordMonth(1, monthStr);
            AmmeterRecordMonthSaveReqVO createReqVO;

            if (null == ammeterRecordMonth) {
                createReqVO = new AmmeterRecordMonthSaveReqVO();
                createReqVO.setAmmeterId(1);
                createReqVO.setRecordMonth(monthStr);
                createReqVO.setHz(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getHz())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageA(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageB(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageC(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityA(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityB(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityC(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setPower(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getPower())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setEng(ammeterRecordDayByMonth.stream().mapToDouble(x -> x.getEng()).sum());
                ammeterRecordMonthService.createAmmeterRecordMonth(createReqVO);
            } else {
                createReqVO = BeanUtils.toBean(ammeterRecordMonth, AmmeterRecordMonthSaveReqVO.class);
                createReqVO.setHz(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getHz())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageA(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageB(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageC(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getVoltageC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityA(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityB(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityC(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getElectricityC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setPower(ammeterRecordDayByMonth.stream().map(x -> BigDecimal.valueOf(x.getPower())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordDayByMonth.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setEng(ammeterRecordDayByMonth.stream().mapToDouble(x -> x.getEng()).sum());
                ammeterRecordMonthService.updateAmmeterRecordMonth(createReqVO);
            }

            // 4.计算电表数据年记录
            String yearStr = LocalDateTimeUtils.getYearStr();
            AmmeterRecordMonthPageReqVO pageRecordMonthReqVO = new AmmeterRecordMonthPageReqVO();
            pageRecordMonthReqVO.setAmmeterId(1);
            pageRecordMonthReqVO.setRecordMonth(yearStr);

            List<AmmeterRecordMonthDO> ammeterRecordMonthByYear = ammeterRecordMonthService.getAmmeterRecordMonthByYear(pageRecordMonthReqVO);
            AmmeterRecordYearDO ammeterRecordYear = ammeterRecordYearService.getAmmeterRecordYear(1, yearStr);
            AmmeterRecordYearSaveReqVO createYearReqVO;

            if (null == ammeterRecordYear) {
                createYearReqVO = new AmmeterRecordYearSaveReqVO();
                createYearReqVO.setAmmeterId(1);
                createYearReqVO.setRecordYear(yearStr);
                createYearReqVO.setHz(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getHz())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageA(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageB(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageC(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityA(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityB(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityC(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createYearReqVO.setPower(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getPower())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createYearReqVO.setEng(ammeterRecordMonthByYear.stream().mapToDouble(x -> x.getEng()).sum());
                ammeterRecordYearService.createAmmeterRecordYear(createYearReqVO);
            } else {
                createYearReqVO = BeanUtils.toBean(ammeterRecordYear, AmmeterRecordYearSaveReqVO.class);
                createYearReqVO.setHz(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getHz())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageA(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageB(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setVoltageC(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getVoltageC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityA(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityA())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityB(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityB())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createReqVO.setElectricityC(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getElectricityC())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createYearReqVO.setPower(ammeterRecordMonthByYear.stream().map(x -> BigDecimal.valueOf(x.getPower())).reduce(BigDecimal.ZERO, BigDecimal::add)
                        .divide(BigDecimal.valueOf(ammeterRecordMonthByYear.size()), 2, RoundingMode.HALF_UP).doubleValue());
                createYearReqVO.setEng(ammeterRecordMonthByYear.stream().mapToDouble(x -> x.getEng()).sum());
                ammeterRecordYearService.updateAmmeterRecordYear(createYearReqVO);
            }

        }
    }
}
